package com.masterclient.app.modules.purchase.adapter;

import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.goods.CommodityAttribute;
import com.eoner.baselibrary.bean.goods.CommodityAttributeOption;
import com.masterclient.app.R;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @auther hjian
 * @date 2020/9/18.
 */
public class PurchaseAttributesAdapter extends BaseQuickAdapter<CommodityAttribute, BaseViewHolder> {

    List<Set<Integer>> attrList;

    public PurchaseAttributesAdapter() {
        super(R.layout.item_purchase_sku_attributes);
        attrList = new ArrayList<>();
    }

    @Override
    protected void convert(BaseViewHolder helper, CommodityAttribute item) {
        helper.setText(R.id.item_purchase_attributes_title, item.getLabel());
        TagFlowLayout attrFlowLayout = helper.getView(R.id.item_purchase_attributes_flow);


        TagAdapter<CommodityAttributeOption> attrAdapter = new TagAdapter<CommodityAttributeOption>(item.getOptions()) {
            @Override
            public View getView(FlowLayout parent, int position, CommodityAttributeOption bean) {
                TextView tv = (TextView) mLayoutInflater.inflate(R.layout.item_purchase_attributes_child,
                        attrFlowLayout, false);
                tv.setText(bean.getTitle());
                return tv;
            }
        };
        attrList.add(new HashSet<>());
        attrFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                attrList.set(helper.getAdapterPosition(), attrFlowLayout.getSelectedList());
                if (onAttrItemClickListener != null) {
                    onAttrItemClickListener.onAttrItemClick(attrList);
                }
                return false;
            }
        });
        attrFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {

            @Override
            public void onSelected(Set<Integer> selectPosSet) {
//                attrList.set(helper.getAdapterPosition(),selectPosSet.)
            }
        });
        attrFlowLayout.setAdapter(attrAdapter);

        //默认选中处理
        if (item.getOptions().size() == 1) {
            attrAdapter.setSelectedList(0);
            attrList.set(helper.getLayoutPosition(), attrFlowLayout.getSelectedList());
        }
        if (helper.getLayoutPosition() + 1 == getItemCount()) {
            if (onAttrItemClickListener != null) {
                onAttrItemClickListener.onAttrItemClick(attrList);
            }
        }
    }

    private OnAttrItemClickListener onAttrItemClickListener;

    public void setOnAttrItemClickListener(OnAttrItemClickListener onAttrItemClickListener) {
        this.onAttrItemClickListener = onAttrItemClickListener;
    }

    public interface OnAttrItemClickListener {
        void onAttrItemClick(List<Set<Integer>> list);
    }
}
